package sg4c.frontend;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import sg4c.frontend.command.GraphicsCommand;
import sg4c.protocol.CapturedInputProtocol;
import sg4c.protocol.DataOutputProtocol;

public class PlaybackGraphicFrontend extends GraphicsFrontend {

    private final CapturedInputProtocol restoreCapturedInput;

    public PlaybackGraphicFrontend(String capturedInput) throws IOException {
        conn = null;
        restoreCapturedInput = new CapturedInputProtocol(new BufferedInputStream(new FileInputStream(capturedInput)));
        backendInput = restoreCapturedInput;
        backendOutput = new DataOutputProtocol() {//a do nothing dummy

            public void writeInt(int val) throws IOException {
            }

            public void writeFloat(float val) throws IOException {
            }

            public void writeDouble(double val) throws IOException {
            }

            public void writeString(String val) throws IOException {
            }

            public void flush() throws IOException {
            }
        };
    }

    @Override
    protected void prepareGraphicCommands() {
        List<GraphicsCommand> commands;
        try {
            commands = (List<GraphicsCommand>) restoreCapturedInput.readObject();
            for (GraphicsCommand cmd : commands) {
                cmd.execute(context);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
            System.exit(1);
        }

    }
}
